import Vue from 'vue';
import Vuex from 'vuex';

import users_u from './users-u';
import dati from './dati';
import user from './user';
import dynamicRouter from '../router/dynamic-router';


Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        routes: [
            {
                path: '/index',
                name: 'index',
                meta: { title: ' 主页' },
                
            },
            {
                path: '/addItem',
                name: 'addItem',
                meta: { title: '新增' },
                
            },
            {
                path: '/userman',
                name: 'userman',
                meta:{title: '用户管理',},
                redirect: {
                    name: 'usere'
                },
                    children:[
                    {
                        path: 'usere',
                        name: 'usere',
                        meta: {
                            title: '企业用户',
                        },
                        component: () =>
                            import('../views/users-e/index.vue')
                    },
                    
            ]
            },
            {
                path: '/testmanage',
                name: 'testmanage',
                meta:{title: '测评管理',},
                component:()=>import('../views/evaluation/index.vue')
            
            },
            {
                path: '/manage',
                name: 'manage',
                meta: { title: '项目管理', },
                redirect: {
                    name: 'usere'
                },
                    children: [
                        {
                            path: 'projectmg',
                            name: 'projectmg',
                            meta: {
                                title: '项目管理',
                            },
                            component: () =>
                                import('../views/projectmg/index.vue')
                        },
                        
                    ]
            },
            {
                path: '/userinfo',
                name: 'userinfo',
                meta: {
                    title: '用户信息',
                },
                component: () =>
                import('../views/user/userinfo.vue')
            },
            
        ],
        get UserToken() {
            return localStorage.getItem('token')
        },
        set UserToken(value) {
            localStorage.setItem('token', value)
        },
        get UserName(){
            return localStorage.getItem('username')
        },
        set UserName(value) {
            localStorage.setItem('username', value)
        },
        get userId(){
            return localStorage.getItem('userId')
        },
        set userId(value) {
            localStorage.setItem('userId', value)
        },
        get phone(){
            return localStorage.getItem('phone')
        },
        set phone(value) {
            localStorage.setItem('phone', value)
        },
        get mail(){
            return localStorage.getItem('mail')
        },
        set mail(value) {
            localStorage.setItem('mail', value)
        },
        get department(){
            return localStorage.getItem('department')
        },
        set department(value) {
            localStorage.setItem('department', value)
        },
        get weChat(){
            return localStorage.getItem('weChat')
        },
        set weChat(value) {
            localStorage.setItem('weChat', value)
        },
        get job(){
            return localStorage.getItem('job')
        },
        set job(value) {
            localStorage.setItem('job', value)
        },
        get address(){
            return localStorage.getItem('address')
        },
        set address(value) {
            localStorage.setItem('address', value)
        },
        get companyId(){
            return localStorage.getItem('companyId')
        },
        set companyId(value) {
            localStorage.setItem('companyId', value)
        },
        

        baseUrl:"http://47.105.186.204:70",
        get permissions(){  
            return JSON.parse(localStorage.getItem('permissions'))
        },
        set permissions(value){  
            localStorage.setItem('permissions',JSON.stringify(value))
        },
        tabName:"1"
    },

    mutations: {
        setUserinfo (state,user) {
          state.userinfo=user
        },
        setTabName(state, tabName) {
            state.tabName = tabName
          }
    },
    modules: {
        users_u,
        dati,
        user
    },
    actions: {
        changeTabName({ commit }, name) {
            commit('setTabName', name)
        },
        async FETCH_PERMISSION({ commit, state }) {
            let permissionList = await fetchPermission()

            /*  根据权限筛选出设置好的路由并加入到path=''的children */
            let routes = recursionRouter(permissionList, dynamicRouter)
            let MainContainer = DynamicRoutes.find(v => v.path === '')
            let children = MainContainer.children
            children.push(...routes)

            /* 生成左侧导航菜单 */
            commit('SET_MENU', children)

            /*
                为所有有children的菜单路由设置第一个children为默认路由
                [
                    /manage/menu1,
                    /manage/menu2
                ]
            */
            setDefaultRoute([MainContainer])

            /*  初始路由 */
            let initialRoutes = router.options.routes

            /*  动态添加路由 */
            router.addRoutes(DynamicRoutes)

            /* 完整的路由表 */
            commit('SET_PERMISSION', [...initialRoutes, ...DynamicRoutes])
        }
    }
});
